package liangtWorkSpace.proxy.cglib.callback;

import liangtWorkSpace.proxy.cglib.aop.ProxyFactory;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

/**
 * @ClassName Test
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/12 16:58
 */
public class Test {

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        Enhancer enhancer = new Enhancer();
        //设置代理对象
        enhancer.setSuperclass(helloWorld.getClass());
//        调用过滤器
        CallbackFilter callbackFilter = new CallBackFilterTest();
        enhancer.setCallbackFilter(callbackFilter);
//        创建各个代理目标的回调，回调顺序要和过滤器一致
        Callback callbacks[] = new Callback[]{new ProxyFactory(), NoOp.INSTANCE};
        enhancer.setCallbacks(callbacks);
//        设置类加载器
        enhancer.setClassLoader(helloWorld.getClass().getClassLoader());

        HelloWorld helloWorld1 = (HelloWorld) enhancer.create();
        System.out.println(helloWorld1.update());
        System.out.println("==============");
        System.out.println(helloWorld1.save());
    }
}
